<?php

if ((int)$this->getCurrentCategory()->getIsAnchor() && 
    !Mage::getStoreConfig('easy_catalogimg/general/anchor')) {
    
    return;        
}

$columnsCount = (int)Mage::getStoreConfig('easy_catalogimg/general/columnsCount');
$_categories = $this->getCurrentChildCategories();
$showImageThumbnails = (bool)Mage::getStoreConfig('easy_catalogimg/general/showImageThumbnails');
$showSubChild = (bool)Mage::getStoreConfig('easy_catalogimg/general/showSubChild');
$height = Mage::getStoreConfig('easy_catalogimg/general/height'); 
$width = Mage::getStoreConfig('easy_catalogimg/general/width'); 
$countSubChild = Mage::getStoreConfig('easy_catalogimg/general/countSubChild');

if (is_array($_categories)) {
    $_collectionSize = count($_categories);
} else {
    $_collectionSize = $_categories->count();
} 
?>
<div class="listing-catalogimg-grid catalogimg-listing">   
    <?php $i=0; foreach ($_categories as $_category): 
        if (!$_category->getIsActive()): continue; endif;
        $cur_category=Mage::getModel('catalog/category')->load($_category->getId());
        $imageUrl = $cur_category->getImageUrl();
        if (empty($imageUrl)): $imageUrl = Mage::getBaseUrl('media').'catalog/category/np_thumb2.gif'; endif;
    ?>
    
    <?php if($i++%$columnsCount==0): ?>
        <ol class="grid-row">
    <?php endif; ?>
        <li class="item">
            <div class="category-entry">
                <?php if ($showImageThumbnails) : ?>
                <a href="<?php echo $_category->getUrl() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="product-image">
                	<?php if(!Mage::getStoreConfig('easy_catalogimg/general/resizeImageThumbnails')){ ?>
                    	<img src="<?php echo $imageUrl ?>" <?php echo !empty($height) ? ' height="' . $height . '"' : ''; echo !empty($width) ? ' width="' . $width . '"' : '' ?> alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
					<?php } else { ?>
						<img src="<?php echo Mage::helper('easycatalogimg/image')->resize($imageUrl, $width , $height) ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
					<?php } ?>
				</a>
                <?php endif; ?>
                <h5><a href="<?php echo $_category->getUrl() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="easycategoriesroot"><?php echo $this->htmlEscape($_category->getName()) ?></a></h5>
				<?php if ($showSubChild) : ?>
                <ul>
                    <?php
                        $j = 0;
                        if (Mage::helper('catalog/category_flat')->isEnabled()) {
                            $subcategories = $_category->getChildrenNodes();
                            
                        } else {
                            $subcategories = $_category->getResource()
                                ->getChildren($_category, false);
                        }
                        $subcategoriesCount = count($subcategories);
                        //$subcategories = $_category->getChildrenNodes();
                        if ($subcategoriesCount) :
                        foreach ($subcategories as $subcategory) :

                            $id = is_object($subcategory) ?
                                $subcategory->getId() : $subcategory;

                            $currentSubCategory = Mage::getModel('catalog/category')
                                ->load($id);
                            if ($j > $countSubChild - 1) : ?>
                                <li>
                                    <a href="<?php echo $cur_category->getUrl() ?>" title="<?php echo $this->htmlEscape($cur_category->getName()) ?>"  class="easycategoriesmore"><?php echo $this->__('More...'); ?></a>
                                </li>

                            <?php
                            break;
                            endif;
                        ?>
                        <li>
                                <a href="<?php echo $currentSubCategory->getUrl() ?>" title="<?php echo $this->htmlEscape($currentSubCategory->getName())?>" class="easycategorieschild"><?php echo $this->htmlEscape($currentSubCategory->getName())?></a>
                        </li>
                    <?php
                        $j++;
                        endforeach;endif;?>
                </ul>
				<?php endif; ?>
            </div>
        </li>   
    <?php if($i%$columnsCount==0 || $i==$_collectionSize): ?>
        </ol>
    <?php endif; ?>
    
    <?php endforeach; ?> 
    <script type="text/javascript">decorateGeneric($$('.grid-row'), ['last', 'odd', 'even']);</script>
</div>